$(function() {
    // 用户点击改变input的属性
    $(".main .password1 i").click(function () {
        var type = $(".main .password1 input").attr("type");
        if (type == "password") {
            $(".main .password1 input").attr("type", "text");
        } else {
            $(".main .password1 input").attr("type", "password");
        }
    });
    var flag;
    //验证手机号if()
    $('#phoneText').blur(function () {
        $("#tel-error").css("display", "none");
        var phone = $('#phoneText').val();
        if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
            $("#tel-error").css("display", "block");
            return false;
            flag = false;
        }
        flag = true;
    });
    // 发送验证码
    $("#codebtn").click(function () {
        if (flag){
            var nums = 90;
            $("#codebtn").attr("disabled","disabled"); //将按钮置为不可点击
            $("#codebtn").val(nums+'秒后可重新获取');
            var clock = setInterval(function () {
                nums--;
                if(nums > 0){
                    $("#codebtn").val(nums+'秒后可重新获取');
                }else{
                    clearInterval(clock); //清除js定时器
                    $("#codebtn").removeAttr("disabled");
                    $("#codebtn").val('点击发送验证码');
                }
            }, 1000); //一秒执行一次
        }
        var phoneText = $('#phoneText').val();
        $.ajax({
            url:BaseUrl+ "/comm/validCode?mobile=" + phoneText,
            type: "get",
            success: function (response) {
                if (response) {
                }
            },
            error: function () {
                alert("系统繁忙，请稍后再试...");
            }
        });
    });
    //验证码验证
    $("#sendCode").blur(function () {
        $("#code-error").css("display", "none");
        var sendCodeOK = $("#sendCode").val();
        if (!(/^\d{4}$/.test(sendCodeOK))) {
            $("#code-error").css("display", "block");
            return false;
        }
    })
    //密码验证
    $("#pasWone").blur(function () {
        $("#pwd-error").css("display", "none");
        var pasWoneOK = $("#pasWone").val();
        if (!(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/.test(pasWoneOK))) {
            $("#pwd-error").css("display", "block");
            return false;
        }
    })
    //确认密码验证
    $("#pasW").blur(function () {
            $("#pwd2-error").css("display", "none");
            var pasWoneOK = $("#pasWone").val();
            var pasWOK = $("#pasW").val();
            if (pasWOK !== pasWoneOK) {
                $("#pwd2-error").css("display", "block");
                return false;
            }
        });

    $("form input").focus(function () {
        $("#all-error").css("display", "none");
    })
//  注册
    $("#regsubmit").click(function () {
        var sendCodeOK = $('#sendCode').val();//验证码
        var phoneTextOK = $('#phoneText').val();//手机号
        var pasWOK = $('#pasW').val();//确认密码
        var pasWone = $('#pasWone').val();//密码
        console.log("手机号：" + phoneTextOK);
        console.log("验证码：" + sendCodeOK);
        console.log("密码：" + pasWOK);
        $("#all-error").css("display", "none");
        if (sendCodeOK == "" || phoneTextOK == "" || pasWOK == "" || pasWone == "") {
            $("#all-error").css("display", "block");
            $("#all-error").html("所有项不能为空");
            return false;
        } else {
            $.ajax({
                url: BaseUrl+ "/api/register",
                type: "post",
                data: {
                    mobile: phoneTextOK,
                    code: sendCodeOK,
                    password: pasWone,
                    password1: pasWOK
                },
                success: function (response) {
                    console.log(response.msg);
                    console.log(response.code);
                    if (response.code == 0) {
                        location.href = 'login.html';
                        // console.log("code的值");
                    } else if (response.code == 103) {
                        alert("user has been registered, please login ahead !");
                        location.href = 'login.html';
                    }
                },
                error: function () {
                    alert("系统繁忙，请稍后再试...");
                }
            });
        }
    });

});